package com.mf.all.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mf.all.emuns.enumsPublic.EnumPublicEnableOrDisable;
import com.mf.all.entity.req.publicReq.updateStatus.ReqPublicIntIdUpdateStatus;
import com.mf.all.entity.req.publicReq.updateStatus.ReqPublicUpdateStatusIntByPid;
import com.mf.all.groups.GroupsAddInterface;
import com.mf.all.groups.GroupsUpdateInterface;
import com.mf.all.result.Result;
import com.mf.all.utils.UtilController;
import com.mf.all.utils.UtilsEnum;
import com.mf.all.utils.tree.UtilTree;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.mf.all.entity.dto.DtoWarehouseArea;
import com.mf.all.entity.req.ReqWarehouseArea;
import com.mf.all.service.ServiceWarehouseArea;
import com.mf.all.entity.req.search.basic.SearchReqWarehouseArea;

import java.util.Arrays;
import java.util.List;

/**
 * 仓库区域
 *
 * @date 2025-04-02 22:36:45
 */
@RestController
@RequestMapping("warehouseArea")
@Tag(name = "仓库区域")
public class ControllerWarehouseArea {
    @Resource
    private ServiceWarehouseArea serviceWarehouseArea;

    @Operation(summary = "分页列表查询")
    @PostMapping("/page")
    @SaCheckPermission("warehouseArea:search")
    public Result<Page<DtoWarehouseArea>> findPage(@RequestBody SearchReqWarehouseArea searchReqWarehouseArea) {
        return Result.success(serviceWarehouseArea.findPageByParams(searchReqWarehouseArea));
    }

    @Operation(summary = "列表查询")
    @PostMapping("/list")
    @SaCheckPermission("warehouseArea:search")
    public Result<List<DtoWarehouseArea>> list(@RequestBody SearchReqWarehouseArea searchReqWarehouseArea) {
        return Result.success(serviceWarehouseArea.findListByParams(searchReqWarehouseArea));
    }

    @Operation(summary = "列表查询(树结构)")
    @PostMapping("/listTree")
    @SaCheckPermission("warehouseArea:search")
    public Result<List<DtoWarehouseArea>> listTree(@RequestBody SearchReqWarehouseArea searchReqWarehouseArea) {
        final LambdaQueryWrapper<DtoWarehouseArea> wrapper = serviceWarehouseArea.fillParams(searchReqWarehouseArea);
        final List<DtoWarehouseArea> list = serviceWarehouseArea.list(wrapper);
        final List<DtoWarehouseArea> dtoWarehouseAreas = UtilTree.buildTree(list);
        return Result.success(dtoWarehouseAreas);
    }

    @Operation(summary = "保存")
    @PostMapping("/save")
    @SaCheckPermission("warehouseArea:save")
    public Result<Void> save(@Validated(GroupsAddInterface.class) @RequestBody ReqWarehouseArea reqWarehouseArea) {
        serviceWarehouseArea.save(UtilController.setBo(reqWarehouseArea, DtoWarehouseArea.class));
        return Result.success();
    }

    @Operation(summary = "修改")
    @PostMapping("/update")
    @SaCheckPermission("warehouseArea:update")
    public Result<Void> update(@Validated(GroupsUpdateInterface.class) @RequestBody ReqWarehouseArea reqWarehouseArea) {
        serviceWarehouseArea.updateById(UtilController.setBo(reqWarehouseArea, DtoWarehouseArea.class));
        return Result.success();
    }

    @Operation(summary = "删除")
    @PostMapping("/delete")
    @SaCheckPermission("warehouseArea:delete")
    public Result<Void> delete(@RequestBody Integer[] ids) {
        serviceWarehouseArea.removeByIds(Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "修改状态")
    @PostMapping("/updateStatus")
    @SaCheckPermission("warehouseArea:updateStatus")
    public Result<Void> updateStatus(@Validated @RequestBody ReqPublicUpdateStatusIntByPid updateStatus) {
        UtilsEnum.existsInEnum(EnumPublicEnableOrDisable.class, updateStatus.getStatus());
        serviceWarehouseArea.updateById(UtilController.setBo(updateStatus, DtoWarehouseArea.class));
        return Result.success();
    }

}
